<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="handleSearchList()" size="small">查询搜索</el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="订单编号:">
            <el-input v-model="listQuery.orderSn" class="input-width" placeholder="订单编号"></el-input>
          </el-form-item>
          <el-form-item label="套餐名称:">
            <el-input v-model="listQuery.mealName" class="input-width" placeholder="套餐名称"></el-input>
          </el-form-item>
          <el-form-item label="订单金额:">
            <el-input v-model="listQuery.price" class="input-width" placeholder="订单金额"></el-input>
          </el-form-item>
          <!--<el-form-item label="开始日期：">-->
            <!--<el-date-picker-->
              <!--class="input-width"-->
              <!--v-model="listQuery.startDate"-->
              <!--value-format="yyyy-MM-dd"-->
              <!--type="date"-->
              <!--placeholder="请选择时间"-->
            <!--&gt;</el-date-picker>-->
          <!--</el-form-item>-->
          <!--<el-form-item label="结束日期：">-->
            <!--<el-date-picker-->
              <!--class="input-width"-->
              <!--v-model="listQuery.endDate"-->
              <!--value-format="yyyy-MM-dd"-->
              <!--type="date"-->
              <!--placeholder="请选择时间"-->
            <!--&gt;</el-date-picker>-->
          <!--</el-form-item>-->
          <el-form-item label="订单状态：">
            <el-select
              v-model="listQuery.orderStatus"
              class="input-width"
              placeholder="全部"
              clearable
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button type="success" icon="el-icon-download" @click="exportToExcel">导出</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="orderTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="订单编号" width="150" align="center">
          <template slot-scope="scope">{{scope.row.orderSn}}</template>
        </el-table-column>
        <el-table-column label="会员名称" align="center" width="100">
          <template slot-scope="scope">{{scope.row.memberName}}</template>
        </el-table-column>
        <el-table-column label="手机号" width="180" align="center">
          <template slot-scope="scope">{{scope.row.phone}}</template>
        </el-table-column>
        <el-table-column label="套餐名称" width="150" align="center">
          <template slot-scope="scope">{{scope.row.mealName}}</template>
        </el-table-column>
        <el-table-column label="使用包间" width="150" align="center">
          <template slot-scope="scope">{{scope.row.machineName}}</template>
        </el-table-column>
        <el-table-column label="下单时间" width="180" align="center">
          <template slot-scope="scope">{{scope.row.createDatetime | formatCreateTime}}</template>
        </el-table-column>
        <el-table-column label="开始时间" width="150" align="center">
          <template slot-scope="scope">{{scope.row.startTime}}</template>
        </el-table-column>
        <el-table-column label="结束时间" width="150" align="center">
          <template slot-scope="scope">{{scope.row.endTime}}</template>
        </el-table-column>
        <el-table-column label="订单金额" width="100" align="center">
          <template slot-scope="scope">{{scope.row.price}}</template>
        </el-table-column>
        <el-table-column label="使用积分" width="100" align="center">
          <template slot-scope="scope">{{scope.row.integral}}</template>
        </el-table-column>

        <el-table-column label="订单状态" width="120" align="center">
          <template slot-scope="scope">{{scope.row.orderStatusName}}</template>
        </el-table-column>
        <!--<el-table-column label="操作" width="200" align="center">-->
          <!--<template slot-scope="scope">-->
            <!--<el-button-->
              <!--size="mini"-->
              <!--type="danger"-->
              <!--@click="handleDeleteOrder(scope.$index, scope.row)"-->
            <!--&gt;删除订单</el-button>-->
          <!--</template>-->
        <!--</el-table-column>-->
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[30,50,100,9999]"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { fetchList, deleteOrder } from "@/api/dms/order";
import { formatDate } from "@/utils/date";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 50,
  orderSn: null,
  studentName: null,
  orderStatus: null,
  startDate: null,
  endDate: null
};
export default {
  name: "orderList",
  components: {},
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      listLoading: true,
      list: null,
      total: null,
      multipleSelection: [],
        tHeader:['订单编号', '会员名称', '手机号', '套餐名称' , '使用包间', '下单时间', '订单金额', '订单状态','使用积分'],
        tValue:['orderSn', 'memberName', 'phone', 'mealName', 'machineName', 'createDatetime','price','orderStatusName','integral'],
      statusOptions: [
        {
          label: "待付款",
          value: 1
        },
        {
          label: "已付款",
          value: 2
        },
        {
          label: "已完成",
          value: 7
        },
        {
          label: "待取消",
          value: 20
        },
        {
          label: "已退款",
          value: 14
        }
        ,
        {
          label: "未付款",
          value: 12
        }
      ],
      bargainOptions:[
        {
          label: "是",
          value: 1
        },
        {
          label: "否",
          value: 0
        }
      ]
    };
  },
  created() {
    this.listQuery.orderStatus = 7
    this.getList();
  },
  filters: {
    formatCreateTime(time) {
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
    formatStatus(value) {
      if (value === 1) {
        return "待付款";
      } else if (value === 2) {
        return "已付款";
      }else if (value === 7) {
        return "已完成";
      }else if (value === 12) {
        return "未付款";
      }else if (value === 14) {
        return "已退款";
      }else if (value === 20) {
        return "待取消";
      }
    }
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleViewOrder(index, row) {
      //this.$router.push({ path: "/oms/orderDetail", query: { id: row.id } });
    },
    handleDeleteOrder(index, row) {
      this.deleteOrder(row.id);
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.records;
        this.total = response.data.total;
      });
    },
    deleteOrder(id) {
      this.$confirm("是否要进行该删除操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let params = new URLSearchParams();
        params.append("id", id);
        deleteOrder(params).then(response => {
          this.$message({
            message: "删除成功！",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      });
    },
      exportToExcel () {
          this.$confirm('确定下载列表文件?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          }).then(() => {
              this.export2Excel()
          }).catch(() => {})
      },
      export2Excel () {
          var that = this
          require.ensure([], () => {
              const { export_json_to_excel } = require('@/assets/js/Export2Excel.js')// js文件的位置
              const tHeader = that.tHeader;
              const filterVal = that.tValue;// 导出的表头字段名，需要导出表格字段名
              const data = that.formatJson(filterVal, that.list);
              export_json_to_excel(tHeader, data, '订单表')// 导出的表格名称，根据需要自己命名
          })
      },
      formatJson (filterVal, jsonData) {
          return jsonData.map(v => filterVal.map(j => v[j]))
      }
  }
};
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>


